package cn.lu.train;

import android.app.Activity;
import android.app.Application;
import android.os.Environment;
import android.os.Process;

import com.baidu.mapapi.SDKInitializer;
import com.mob.MobSDK;


import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import cn.lu.train.entry.User;
import cn.lu.train.util.LogUtils;

/**
 * Created by luxianyue on 2018/3/12.
 */

public class App extends Application  {

    public static String versionName = BuildConfig.VERSION_NAME;
    public static int versionCode = BuildConfig.VERSION_CODE;
    public static String saveImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/joke/images";
    public static String editImageOutputPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/joke/editImage";

    private static App mApp;
    public static boolean isIMConnected;

    private User mUser;
    public long userIdentify;
    public String userToken;

    @Override
    public void onCreate() {
        super.onCreate();
        //smssdk
        MobSDK.init(this);

        LogUtils.setDebug(true);
        mApp = this;

        SDKInitializer.initialize(getApplicationContext());

    }

    public static void setUser(User User) {
        mApp.mUser = User;
    }

    public static User getLoginUser() {
        return mApp.mUser;
    }

    public static String userHeadImage() {
        if (mApp.mUser != null) {
            return mApp.mUser.head_img;
        }
        return null;
    }

    public static long getUserId() {
        if (mApp.mUser != null) {
            return mApp.mUser.id;
        }
        return mApp.userIdentify;
    }

    public static String getUserToken() {
        if (mApp.mUser != null) {
            return mApp.mUser.token;
        }
        return mApp.userToken;
    }

    public static App get() {
        return mApp;
    }

    public void initPath() {
        File file = new File(saveImagePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        file = new File(editImageOutputPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        File dataCache = new File(getCacheDir().getAbsolutePath() + "/video");
        if (!dataCache.exists()) {
            dataCache.mkdir();
        }

        File thumb = new File(dataCache, "thumb");
        if (!thumb.exists()) {
            thumb.mkdir();
        }
    }

    public void addAy(Activity ay) {
    }

    public void removeAy(Activity ay) {
    }

    public void exit() {
        Process.killProcess(Process.myPid());
    }

    public void uncaughtException(Thread thread, Throwable ex) {
        if (ex == null) {
            exit();
            return;
        }
        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        ex.printStackTrace(printWriter);
        LogUtils.e(writer.toString());
        exit();
/*        try {
            File eFile = getDir("error", MODE_PRIVATE);
            if (!eFile.isDirectory()) {
                return;
            }
            String path = eFile.getAbsolutePath();
            CharSequence appName = getApplicationInfo().loadLabel(getPackageManager());
            eFile = new File(path + "/" + appName + "_error.txt");
            if (eFile.exists()) {
                eFile.createNewFile();
            }

            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            ex.printStackTrace(printWriter);
            LogUtils.e(writer.toString());

            FileOutputStream fos = new FileOutputStream(eFile, true);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            long time = System.currentTimeMillis();
            SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String strTime = myFmt.format(new Date(time));
            strTime = strTime
                    + "\nAppName: " +getPackageManager().getApplicationLabel(getApplicationInfo())
                    + "\nPackageName: " + getPackageName() + "\n";
            osw.write(strTime + writer.toString() + "\n---------------------------------------------------------\n");
            osw.flush();

            osw.close();
            fos.close();
            writer.close();
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(App.this, "很抱歉,程序出现异常,即将退出", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }.start();
            SystemClock.sleep(2500);
            exit();
        }*/
    }
}
